package com.purplesoft.zettl.Utilities;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.purplesoft.zettl.R;
import com.purplesoft.zettl.Database.DatabaseExecuter;
import com.purplesoft.zettl.DatabaseTables.TblCart;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;

public class ProgressBarItems {
	
	private  Cursor csVwPurchaseItems, csTblCart;
	private DatabaseExecuter dbExec;
	private SharedPrefs sharedPrefs;
	
	public void calculateItems(Context context) {
		
		Activity activity = (context instanceof Activity) ? (Activity) context : null;
		ProgressBar pbItems = (ProgressBar) activity.findViewById(R.id.pbItems_Zettl);
		TextView tvProgressItems = (TextView) activity.findViewById(R.id.tvProgressItems_Zettl);
		
		dbExec = new DatabaseExecuter(context);
		sharedPrefs = new SharedPrefs(context);

//		csVwPurchaseItems = context.getContentResolver().query(DatabaseContentProvider.VWPurchaseItems, null, null, null, null);
		csVwPurchaseItems = dbExec.queryFromVwPurchaseItems(VwPurchaseItems.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), -99));
		csVwPurchaseItems.moveToFirst();
//		csTblCart = context.getContentResolver().query(DatabaseContentProvider.TBLCart, null, null, null, null);
		csTblCart = dbExec.queryFromTblCart(TblCart.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), -99));
		csTblCart.moveToFirst();
		
		// berechne prozentualen Fortschritt des Fortschrittbalkens
		int purchaseItemCount = csVwPurchaseItems.getCount();
		int cartCount = csTblCart.getCount();
		
		int totalCount = purchaseItemCount + cartCount;
		double progress = (double) cartCount/ (double) totalCount;
		progress = Math.round(progress*100);
		
		tvProgressItems.setText(cartCount + "/" + totalCount + " " + context.getResources().getString(R.string.items_plain));
		pbItems.setProgress((int) progress);

		
	}

}
